package main

import "fmt"

func main() {
	fmt.Println(maxSubarraySumCircular([]int{5, -3, 5}))
}

func maxSubarraySumCircular(nums []int) int {
	dp := make([]int, len(nums))
	dp[0] = nums[0]
	max := nums[0]
	addHead := true
	for i := 1; i < len(nums); i++ {
		if i == len(nums)-1 && dp[0] > 0 {
			if addHead && dp[i-1] > dp[0] {
				dp[i] = nums[i] + dp[i-1]
			} else {
				dp[i] = nums[i] + dp[i-1]
			}
		} else if dp[i-1] > 0 {
			dp[i] = dp[i-1] + nums[i]
		} else {
			addHead = false
			dp[i] = nums[i]
		}

		if dp[i] > max {
			max = dp[i]
		}
	}
	return max
}
